﻿using Flame.Controls;
using Flame.Dlr;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flame.Gui.IronPython
{
    [Flame.Controls.GuiPlugin]
    public static class GuiPluginPyLoader
    {
        static void exec(IExecutable e, string path)
        {
            dynamic d = e.Execute(File.ReadAllText(path));
            if (d.Data is Exception)
                throw d.Data as Exception;
        }


        public static void Load(string path, ScripterControlFormInfo scripterControlFormVariable)
        {
            IExecutable e = Manager.GetNewEngine("ipy");
            AssemblyHelper.Apply(AssemblyHelper.CurrentAppDomainAssemblies, e);
            e.AddVariable(new Variable() { Name = "flame", Data = scripterControlFormVariable });

            foreach (var py in Directory.EnumerateFiles(path, "*.py"))
            {
                exec(e, py);
            }
        }
    }
}
